package com.sample.plove.database.service.impl;

import com.sample.plove.database.dao.UserRepository;
import com.sample.plove.database.model.UserEntity;
import com.sample.plove.database.service.UserServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * PackageName:com.sample.plove.database.service.impl
 * Author:Administrator
 * Time:22:41
 * Date:2019/5/18
 * Describe:用户信息数据库操作实现类
 */
@Repository
public class UserServerImpl implements UserServer {
    private static final Logger log = LoggerFactory.getLogger("UserServer");
    @Autowired
    private UserRepository userRepository;

    /**
     * 添加用户信息
     * @param entity 用户实体
     */
    @Override
    public void addUser(UserEntity entity) {
        log.info("");
        userRepository.save(entity);
    }

    /**
     * 通过id查询用户信息
     * @param id 查询的id
     * @return 返回的用户信息
     */
    @Override
    public Optional<UserEntity> findUserById(int id) {
        log.info("");
        return userRepository.findById(id);
    }

    /**
     * 查询所有用户信息
     * @return 返回的用户信息集合
     */
    @Override
    public List<UserEntity> findAllUser() {
        log.info("");
        return userRepository.findAll();
    }

    /**
     * 更新用户信息
     * @param entity 更新的信息
     */
    @Override
    public void updateUser(UserEntity entity) {
        log.info("");
        userRepository.save(entity);
    }

    /**
     * 删除指定的用户信息
     * @param entity 需要删除的用户对象
     */
    @Override
    public void removeUser(UserEntity entity) {
        log.info("");
        userRepository.delete(entity);
    }

    /**
     * 清空用户信息表
     */
    @Override
    public void cleanUser() {
        log.info("");
        userRepository.deleteAll();
    }
}
